"""
@name : 14.面向对象的练习
@author : wenyao
@projectname: sanchuanglianxi
"""

#ATM 账户资金交易管理
#登录
#查看
#取钱
#存钱
#打印出交易详情

#设计
#静态属性
#钱    实例属性
#账户名  实例属性
#密码    实例属性
#当前时间  类属性
#交易记录   实例属性

#方法
#登录
#取钱
#存钱
#查看账户余额
#打印交易详情
#退出登录

#主函数

import time
import re

class ATM():
    def __init__(self, money, account):
        self.money = money
        self.account = account
        self.change_log = []

    def login(self):
        local_account = {"account":"wen", "passwd":'123456'}
        i = 0
        while i<3:
            passwd = input("请输入账户密码:")
            if passwd == local_account.get("passwd"):
                print("登录成功!!")
                return True
            else:
                print("密码错误，请重新输入")
                i += 1
        if i == 3:
            print("密码输错3次，自动退出")
            return False
    def get_money(self):
        money = input("请输入要取的金额:")
        ans = re.findall('(^\d+$)|(^\d+\.\d+$)', money)
        if ans[0][0] !=""or ans[0][1]!= "":
            money = float(money)
            if money <= self.money:
                tmp_dict = {}
                self.money -= money
                tmp_dict["money"] = money
                tmp_dict["time"] = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
                tmp_dict["action"] = "取钱"
                tmp_dict["balance"] = self.money
                self.change_log.append(tmp_dict)
            else:
                print("余额不足！！！！！")
    def save_money(self):
        money = input("请输入要存的金额:")
        ans = re.findall('(^\d+$)|(^\d+\.\d+$)', money)
        if ans[0][0] != "" or ans[0][1] != "":
            money = float(money)
            self.money += money
            tmp_dict = {}
            tmp_dict["money"] = money
            tmp_dict["time"] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            tmp_dict["action"] = "存钱"
            tmp_dict["balance"] = self.money
            self.change_log.append(tmp_dict)
    def show(self):
        print("当前余额为：",self.money)
    def change_print(self):
        if len(self.change_log)> 0:
            for tmp_dict in self.change_log:
                print("操作时间为:{time},"
                      "{action},"
                      "操作金额:{money},"
                      "余额:{balance}".format(**tmp_dict))
        else:
            print("当前交易记录为空！")

    def sys_exit(self):
        exit(0)

    def run(self):
        if self.login():
            tmp_dict = {"a": "get_money", "b": "save_money",
                        "c": "show","d":"change_print","e":"sys_exit"}
            while True:
                ans = input("请输入你的选择， a:取钱  b:存钱  c:查看余额  d:打印交易详情  e:退出登录")
                if ans in ["a","b","c","d","e"]:
                    # func_attr = tmp_dict.get(ans)
                    # func_attr()
                    if hasattr(self,tmp_dict.get(ans)):
                        func_attr = getattr(self,tmp_dict.get(ans))
                        func_attr()

if  __name__ == "__main__":
    a = ATM(1000000,"wen")
    a.run()









